-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--[[

    蜂箱升级

]]--
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- bee go home event fn
    local function bee_go_home_event_fn(inst,data)
        local child = data and data.child
        if child and child.prefab == "bee" and child.components.pollinator then
            local bee = child
            -- local flowers = bee.components.pollinator.flowers
            for k, temp_flower in pairs(bee.components.pollinator.flowers) do
                if temp_flower.prefab then
                    inst:PushEvent("hutao_event.bee_pollinated",temp_flower.prefab)
                end
            end
        end
    end
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
---
    local function workable_test_fn(inst,doer,right_click)
        if inst:HasTag("burnt") then
            return false
        end
        if inst.replica.hutao_com_bee_box_ex_harvest and inst.replica.hutao_com_bee_box_ex_harvest:HasAnyExResult() then
            return true
        end
        return false
    end    
    local function workable_replica_init(inst,replica_com)
        -- replica_com:SetText("beebox",STRINGS.ACTIONS.HARVEST .."  ".. TUNING.HUTAO_FNS:GetString("hutao_cooked_food_plum_blossom_wine","name"))
        replica_com:SetText("beebox",STRINGS.ACTIONS.HARVEST .." ".. STRINGS.UI.EMOTES.UNLOCKABLES)
        replica_com:SetTestFn(workable_test_fn)
    end
    local function on_work_fn(inst,doer)
        local ex_results = inst.components.hutao_com_bee_box_ex_harvest:GetExResult()
        if #ex_results <= 0 then
            return false
        end
        for k, item_prefab in pairs(ex_results) do
            if PrefabExists(item_prefab) then
                doer.components.inventory:GiveItem(SpawnPrefab(item_prefab))
            end
        end
        inst.components.hutao_com_bee_box_ex_harvest:ClearExResult()
        inst.components.hutao_com_bee_box_ex_harvest:ClearAllPollinatedFlowers()
        return true
    end
    local function workable_com_install(inst)
        inst:ListenForEvent("HUTAO_OnEntityReplicated.hutao_com_workable",workable_replica_init)
        if not TheWorld.ismastersim then
            return
        end
        inst:AddComponent("hutao_com_workable")
        inst.components.hutao_com_workable:SetOnWorkFn(on_work_fn)
    end
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- bee_pollinated_event 蜜蜂采集事件。注意，每次蜜蜂回家会重复触发。
    local function bee_pollinated_event(inst,flower_prefab)
        --[[
            蜜蜂每次对梅花跟莓花采蜜，都会累积生成梅花酿的概率。
            每采集一次梅花，会累积0.07%，最多累积7%。
            采集莓花累积0.21%，最多累积21%（两者累加，最多28%）。
            蜜蜂每次回蜂房的时候，若蜂房产量没有满，蜂房会记录身上采集过的花，
            根据上面概率算法，ROLL点一次，若判定成功，则生成一份【梅花酿】。若蜂房已满，则不会生成。
            每次采集蜂房内蜂蜜时，会取走储存的所有梅花酿

                    ["hutao_plant_plum_blossom"] = "plum_blossom",  -- 梅花
                    ["hutao_plant_berry_flower"] = "berry_flower",  -- 莓花
        ]]
        -- if inst.components.hutao_com_bee_box_ex_harvest:HasAnyExResult() then
        --     return
        -- end        
        if inst.components.harvestable and inst.components.harvestable.produce >= inst.components.harvestable.maxproduce then
            return
        end

        inst.components.hutao_com_bee_box_ex_harvest:PollinateFlower(flower_prefab)
        local pollinated_flowers = inst.components.hutao_com_bee_box_ex_harvest:GetAllPollinatedFlowers() or {}        

        local hutao_plant_plum_blossom = pollinated_flowers["hutao_plant_plum_blossom"] or 0
        local hutao_plant_berry_flower = pollinated_flowers["hutao_plant_berry_flower"] or 0
        if hutao_plant_berry_flower + hutao_plant_plum_blossom <= 0 then
            return
        end
        local plum_blossom_precent = math.clamp(hutao_plant_plum_blossom * 0.0007,0,0.07)
        local berry_flower_precent = math.clamp(hutao_plant_berry_flower * 0.00021,0,0.21)
        local total_precent = plum_blossom_precent + berry_flower_precent
        if math.random() <= total_precent or TUNING.HUTAO_DEBUGGING_MODE then            
            inst.components.hutao_com_bee_box_ex_harvest:AddExResult("hutao_cooked_food_plum_blossom_wine")
            -- print("fake error 额外采集生成")
        end
    end
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
AddPrefabPostInit(
    "beebox",
    function(inst)
        workable_com_install(inst)
        if not TheWorld.ismastersim then
            return
        end
        inst:ListenForEvent("childgoinghome",bee_go_home_event_fn)
        inst:ListenForEvent("hutao_event.bee_pollinated",bee_pollinated_event)
        inst:AddComponent("hutao_com_bee_box_ex_harvest")
    end
)


